package org.openmore.platform.pay;

import com.alipay.api.AlipayClient;
import com.github.binarywang.wxpay.config.WxPayConfig;
import org.openmore.common.enums.PaymentType;
import org.openmore.platform.dto.common.BaseResponse;
import org.openmore.platform.entity.WechatPayConfig;
import org.openmore.platform.pay.IAP.IAPPayService;
import org.openmore.platform.pay.alipay.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaymentManager{

    Map<PaymentType,Class<? extends PaymentService>> services;

    @Autowired
    private ApplicationContext context;

    @PostConstruct
    public void init(){
        services = new HashMap<>();
        services.put(PaymentType.ALIPAY, AliPayService.class);
        services.put(PaymentType.WECHAT,WechatPayService.class);
        services.put(PaymentType.WECHAT_MINIAPP,WechatPayService.class);
        services.put(PaymentType.WECHAT_H5,WechatPayService.class);
        services.put(PaymentType.WECHAT_PAD,WechatPayService.class);
        services.put(PaymentType.IAP, IAPPayService.class);
    }

    /**
     * @param payment : 支付参数
     * @param host : 阿里云所需当前主机
     * @param tenantId : 应用id
     * */
    public BaseResponse payment(Payment payment, String host, String tenantId) throws Exception {
        PaymentService paymentService = context.getBean(services.get(payment.getPayType()));
        return paymentService.payment(host, tenantId, payment);
    }
}